//
//  main.cpp
//  EMT_V2_Crisis
//
//  Created by Duong Ngo on 6/5/17.
//  Copyright © 2017 Duong Ngo. All rights reserved.
//

#include <iostream>
#include "global.hpp"
#include "mapping.hpp"
#include "read_steady_state.hpp"
#include "foc_problem.hpp"
#include "homotopy.hpp"

int main()
{
    // Global mapping and vector showing position
    find_original_map(original_map);
    find_equation_map(eqn_map);
    find_positions_of_state_and_forward_variables(state_position, forward_position, var_role);

    // Read steady state
    std::vector<double> ss(var_foc);     //Steady State
    read_steady(ss);
    print_vector(ss);

    
    
    // Initial guess
    std::vector<double> x_init((T+1)*var_foc);
    initial_guess_as_steady_state(ss, x_init);
    
    // Starting state
    starting_state[0] = ss[original_map.at("s.n")];
    starting_state[1] = ss[original_map.at("s.m")];
    starting_state[2] = ss[original_map.at("s.bh")];
    starting_state[3] = ss[original_map.at("s.k")];
    starting_state[4] = ss[original_map.at("s.Rm")];
    starting_state[5] = ss[original_map.at("s.x")];
    starting_state[6] = ss[original_map.at("s.Rn")];
    
    // Exogenous shock
    std::vector<double> kappa_start(T+1), kappa_end(T+1);
    std::vector<double> x_start(T+1), x_end(T+1);
    std::vector<double> rn_start(T+1), rn_end(T+1);
    
    //Mode to indicate which result that users want to generate
    //mode = 0 - LSAP without changing Rn.
    //mode = 1 - LSAP with changing Rn after T_zero periods (only runs after getting result from mode=0
    
    int mode=1;
    
    if (mode==0){
        homotopy_from_ss_to_homo_kappa(ss, kappa_start, kappa_end, x_start, x_end, rn_start, rn_end, x_init);
        homotopy_from_homo_kappa_to_kappa(ss, kappa_start, kappa_end, x_start, x_end, rn_start, rn_end, x_init);
    }

    
    if (mode==1){
        // Homotopy adjusting Rn after 20, 40, 80 (T_zero) periods or not raising Rn (T_zero > T)
        int T_zero = 20; // After T_zero periods, central bank raises Rn
        std::string fname2 ("//Users//duongngo//Documents//Research//E-Monetary Theory//Paper_and_code//EMT_V6//C code//Output//EMT_LSAP_without_adjusting_rn.txt");
        read_guess_from_previous_homotopy(fname2, x_init);
        homotopy_for_vec_rn(T_zero, ss, kappa_start, kappa_end, x_start, x_end, rn_start, rn_end, x_init);
    }
    



    
 
    return 0;
}
